package com.jly.service.impl;

import com.jly.anno.LogMonitor;
import com.jly.loadbalancer.LoadBalancer;
import com.jly.service.AirPlaneService;
import com.jly.vos.ApiResult;
import com.jly.vos.GuidanceDataVo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;

/**
 * @author Liwenhao
 * @date 2022/3/24 11:09
 * @description
 */
@Service("airPlaneServiceHTTPImpl")
@ConditionalOnExpression("'${dev.request}'.equals('http')")
public class AirPlaneServiceHTTPImpl implements AirPlaneService {

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private LoadBalancer loadBalancer;

    @Override
    public ApiResult tackOffAirplane(Long id) {
        return ApiResult.success("飞机起飞");
    }

    @Override
    public ApiResult landAirplane(Long id) {
        return ApiResult.success("飞机着落");
    }

    @Override
    public ApiResult changeAirplaneSpeed(Long id, Double speed) {
        return ApiResult.success("飞机变速");
    }

    @SneakyThrows
    @Override
    @LogMonitor
    public ApiResult attack(GuidanceDataVo request) {
        URI uri = new URI("http://consul-provider-missile-8083/missile/fire");
        ResponseEntity<GuidanceDataVo> guidanceDataVoResponseEntity = restTemplate.postForEntity(uri, request, GuidanceDataVo.class);
        GuidanceDataVo body = guidanceDataVoResponseEntity.getBody();
        return ApiResult.success(body);
    }

    @SneakyThrows
    @Override
    @LogMonitor
    public ApiResult<String> survey(Long id) {
        URI uri = new URI("http://localhost:8085/radar/detect/" + id);
        ApiResult<String> apiResult = restTemplate.getForObject(uri, ApiResult.class);
        return apiResult;
    }


    @Override
    public ApiResult surveyv2(Long id) {

        return null;
    }


}
